
cap log close
log using "$logs/an-ocr.log", replace
	
	use "$data/1216_countylevel_clean.dta", clear
	
/********************************************************************************
	Merge data
********************************************************************/
	
	// Merge with counties that have SEDA Gap data
	merge 1:1 countyid using "$data/sedagaps", keep(3) nogen
	
	// Merge with sample 
	merge 1:1 countyid using "$data/commonsamp" 
	tab _merge // 78% of counties
	keep if _merge == 3
	drop _merge
	
	// Merge with covariates
	merge 1:1 countyid using "$data/seda_covariates_clean", assert(2 3) nogen keep(3) 
	
	// Merge with bias
	merge 1:1 countyid using "$data/mrp_data", assert(2 3) nogen keep(3)

// Prep data for analysis

	tostring countyid, gen(state)
	assert length(state)==4|length(state)==5
	gen temp = substr(state,1,1) if length(state)==4
	replace temp = substr(state,1,2) if length(state)==5
	drop state
	rename temp state
	destring state, replace
	tab state, gen(state_)
	
	gen origtchimpbias = tch_imp_bias
	gen origtchexpbias = tch_exp_bias
	foreach var in tch_imp_bi all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
		egen temp = std(`var')
		replace `var' = temp
		drop temp
		}
		
		foreach var in iss oos {
			foreach r in bl wh {
				gen pr_`var'_`r' = `var'_`r'_all / enr_`r'_all
				}
				gen diff_pr_`var' = pr_`var'_bl-pr_`var'_wh
				}
		sum diff_*
		drop pr_iss_bl-diff_pr_oos
		
	expand 2
	bys countyid: gen black = _n-1
	expand 2
	bys countyid black: gen susp = _n-1
		
	order black susp
	
	gen iss = .
	gen oos = .
	
	foreach var in iss oos {
		replace `var' = `var'_bl_all if black==1 & susp==1
		replace `var' = `var'_wh_all if black==0 & susp==1
		replace `var' = enr_bl_all-`var'_bl_all if black==1 & susp==0
		replace `var' = enr_wh_all-`var'_wh_all if black==0 & susp==0
		}
	order iss oos
	
	// Does this look right..?
	bys susp black: sum oos iss
	
/********************************************************************************
	ANALYZE
********************************************************************/

est clear

	eststo: logit susp black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)		
		
	eststo: logit susp black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)		
		
	eststo: logit susp black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)				
		
	eststo: logit susp black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)	
		
	esttab using "$output/all-ocr.csv", se r2 ar2 drop(state_* sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)
		
	keep if tchsamp
	est clear
	
	eststo: logit susp black all_imp_bias c.all_imp_bias#black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)				
	eststo: logit susp black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)				
		
	eststo: logit susp black all_imp_bias c.all_imp_bias#black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)				
	eststo: logit susp black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_imp_bias c.tch_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)		

	eststo: logit susp black all_exp_bias c.all_exp_bias#black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)				
	eststo: logit susp black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=iss], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)				
		
	eststo: logit susp black all_exp_bias c.all_exp_bias#black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)				
	eststo: logit susp black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		state_* [fw=oos], cluster(countyid)
	eststo: logit susp black tch_exp_bias c.tch_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)		
		
	esttab using "$output/tch-ocr.csv", se r2 ar2 drop(state_* sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)	
		
		
// Figures

	keep if tchsamp
	est clear
	
	cap drop tag
	egen tag = tag(countyid)
	
	sum origtchimpbias if tag
	local mean=r(mean)
	
	logit susp c.origtchimpbias##black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)
		
		mgen, at(origtchimpbias=(.05(.05).45) black=(1)) stub(prsusp1)
		mgen, at(origtchimpbias=(.05(.05).45) black=(0)) stub(prsusp0)
		
		twoway (connected prsusp1pr1 prsusp1origtchimpbias, mcolor(black) lcolor(black) xline(`mean', lcolor(black))) ///
			(connected prsusp0pr1 prsusp0origtchimpbias, mcolor(black) lcolor(black) lpattern(dash) xline(.15, lcolor(black) lpattern(dash))) ///
			(rarea prsusp1ll1 prsusp1ul1 prsusp1origtchimpbias, color(black%30)) ///
			(rarea prsusp0ll1 prsusp0ul1 prsusp0origtchimpbias, color(gray%30)), ///
			legend(order(1 "Black" 2 "White" 3 "Black 95%CI" 4 "White 95%CI"))ytitle(Probability of ISS) xtitle(Teacher Implicit Bias) ///
				yscale(range(0 .25)) ylabel(0(.05).25) xscale(range(0 .5)) xlabel(0(.1).5) ///
				name(iss_imp_tch, replace)
		drop pr*
		
	logit susp c.origtchimpbias##black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)
		
		mgen, at(origtchimpbias=(.05(.05).45) black=(1)) stub(prsusp1)
		mgen, at(origtchimpbias=(.05(.05).45) black=(0)) stub(prsusp0)
		twoway (connected prsusp1pr1 prsusp1origtchimpbias, mcolor(black) lcolor(black) xline(`mean', lcolor(black))) ///
			(connected prsusp0pr1 prsusp0origtchimpbias, mcolor(black) lcolor(black) lpattern(dash) xline(.15, lcolor(black) lpattern(dash))) ///
			(rarea prsusp1ll1 prsusp1ul1 prsusp1origtchimpbias, color(black%30)) ///
			(rarea prsusp0ll1 prsusp0ul1 prsusp0origtchimpbias, color(gray%30)), ///
			legend(order(1 "Black" 2 "White" 3 "Black 95%CI" 4 "White 95%CI"))ytitle(Probability of OOS) xtitle(Teacher Implicit Bias) ///
				yscale(range(0 .25)) ylabel(0(.05).25) xscale(range(0 .5)) xlabel(0(.1).5) ///
				name(oos_imp_tch, replace)
		drop pr* tag
				
	graph combine iss_imp_tch oos_imp_tch		
	gr export "$graphs/tch_imp_bias_marginsplot_oos_iss.png", replace
	graph drop _all

log close

/*
cap log close
log using "$output/an-ocr-margins.log", replace
// Margins
	egen tag = tag(countyid)
	sum origtchimpbias if tag
	local mean = r(mean)
	
	logit susp black origtchimpbias c.origtchimpbias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=iss], cluster(countyid)		

	margins black, at((mean) _all) at(origtchimpbias =(0 `mean')) 
	
	logit susp black origtchimpbias c.origtchimpbias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=oos], cluster(countyid)		

	margins black, at((mean) _all) at(origtchimpbias =(0 `mean')) 
	
log close
